<?php


namespace App\Logic;


use App\Models\News;
use App\server\EsServer;
use mysql_xdevapi\Exception;

class InformationLogic
{
    /**
     * 资讯信息展示列表
     */
    public static function Secondary(){
        return News::list();
    }
    /**
     * 资讯信息添加
     */
    public static function SecondaryAdd($request){
        $validated = $request->validate([
            'title' => 'required',
            'content' => 'required',
            'author' => 'required',
        ],[
            'title.required'=>'不能为空',
            'content.required'=>'不能为空',
            'author.required'=>'不能为空',
        ]);
        $SecondaryModel=News::create($validated);
        return $SecondaryModel;
    }
    /**
     * 资讯信息删除
     */
    public static function SecondaryDel($fangId){
        if(empty($fangId))throw new \Exception('未接收到想要删除的房源信息');
        $executeDel=News::del('id',$fangId);
        return $executeDel;
    }
    /**
     * 资讯信息查找
     */
    public static function SecondarySeek($query){
        if(empty($query)){
            return News::list();
        }
        $executeSeek=News::Seek('title',$query);
        return $executeSeek;
    }
    /**
     * 资讯详情
     */
    public static function SecondaryDetails($fangID){
        if(empty($fangID))throw new \Exception('未接收到想要查看的房源信息');
        $executeDetail=News::detail('id',$fangID);
        return $executeDetail;
    }
    /**
     * 资讯修改
     */
    public static function SecondaryUpdated($SecondaryMessageUpdated){
        unset($SecondaryMessageUpdated['updated_at']);
        unset($SecondaryMessageUpdated['created_at']);
        unset($SecondaryMessageUpdated['deleted_at']);
        $id=$SecondaryMessageUpdated['id'];
        $SecondaryModel=News::change('id',$id,$SecondaryMessageUpdated);
        return $SecondaryModel;
    }
    /**
     * 资讯es搜索
     */
    public static function SecondarySearch($query){
        if(empty($query)){
            return News::list();
        }
        $executeEsSearch=new EsServer();
        $EsMessage['data']=$executeEsSearch->searchEs('fang',$query,'fang_name');
        return $EsMessage;
    }
}
